//
//  GFDetialCommentResturtantTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFDetialCommentResturtantTableViewCell.h"

@interface GFDetialCommentResturtantTableViewCell ()
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UIImageView *foodImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *foodNameLabel;
@property (nonatomic, strong) UIImageView *starImageView;
@property (nonatomic, strong) UILabel *commentLabel;
@property (nonatomic, strong) UILabel *levelLabel;
@end

@implementation GFDetialCommentResturtantTableViewCell
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatImageView];
        [self creatLabel];

    }
    return self;
}
#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    self.userImageView.frame = CGRectMake(5, 5, WIDTH / 7, WIDTH / 7);
    CGRect rectForName = [self.model.user_name boundingRectWithSize:CGSizeMake((WIDTH - 10 - WIDTH / 7 * 2) / 1.2 - 5, 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.nameLabel.frame = CGRectMake(10 + WIDTH / 7, 5, rectForName.size.width, WIDTH / 20);
    
    self.foodImageView.frame = CGRectMake(WIDTH - 5 - WIDTH / 7, 5, WIDTH / 7, WIDTH / 7);
    CGRect rectForFood = [self.model.food_name boundingRectWithSize:CGSizeMake((WIDTH - 10 - WIDTH / 7 * 2) / 1.5, 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.foodNameLabel.frame = CGRectMake(10 + WIDTH / 7, 5 + WIDTH / 20, rectForFood.size.width, WIDTH / 20);
    
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:300 + i];
        imageView.frame = CGRectMake(10 + WIDTH / 7 + WIDTH / 20 * i, 5 + WIDTH / 10, WIDTH / 20, WIDTH / 20);
    }
    CGRect rectForComment = [self.model.comment boundingRectWithSize:CGSizeMake(WIDTH - 10, WIDTH / 4) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    self.commentLabel.frame = CGRectMake(5, 5 + WIDTH / 10 + WIDTH / 20, rectForComment.size.width, rectForComment.size.height);
    CGRect rectForLevel = [self.model.level_title boundingRectWithSize:CGSizeMake(WIDTH - 20 - WIDTH / 7 * 2 - rectForName.size.width , 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
    self.levelLabel.frame = CGRectMake(15 + WIDTH / 7 + rectForName.size.width, 5, rectForLevel.size.width, WIDTH / 20);
    
}
#pragma mark - set
- (void)setModel:(GFCommentResturtantModel *)model {
    if (_model != model) {
        _model = model;
    }
    //头像
    [self.userImageView setImageWithURL:[NSURL URLWithString:model.user_image] placeholderImage:[UIImage imageNamed:@"category_1"]];
    //姓名
    self.nameLabel.text = model.user_name;
    //食物图片
    [self.foodImageView setImageWithURL:[NSURL URLWithString:model.picture_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    //食物名称
    self.foodNameLabel.text = model.food_name;
    //评分星级
    NSString *star = [NSString stringWithFormat:@"%.1f", model.star];
    NSRange rangeForFloat = {2,1};
    NSRange rangeForInt = {0,1};
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:300 + i];
        imageView.image = [UIImage imageNamed:@"wuxing.png"];
        NSString *strTail = [star substringWithRange:rangeForFloat];
        NSString *strHead = [star substringWithRange:rangeForInt];
        if ([strTail isEqualToString:@"0"]) {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:300 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
        } else {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:300 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
            UIImageView *imageView3 = (UIImageView *)[self.contentView viewWithTag:300 + [strHead integerValue]];
            imageView3.image = [UIImage imageNamed:@"banxing.png"];
        }
        
    }
    //评论
    self.commentLabel.text = model.comment;
    //等级
    self.levelLabel.text = model.level_title;
    //等级颜色
    switch (model.level_id) {
        case 0:
            self.levelLabel.backgroundColor = [UIColor blueColor];
            break;
        case 1:
            self.levelLabel.backgroundColor = [UIColor brownColor];
            break;
        case 2:
            self.levelLabel.backgroundColor = [UIColor magentaColor];
            break;
        case 3:
            self.levelLabel.backgroundColor = [UIColor cyanColor];
            break;
        case 4:
            self.levelLabel.backgroundColor = [UIColor greenColor];
            break;
        case 5:
            self.levelLabel.backgroundColor = [UIColor grayColor];
            break;
        case 6:
            self.levelLabel.backgroundColor = [UIColor yellowColor];
            break;
        case 7:
            self.levelLabel.backgroundColor = [UIColor orangeColor];
            break;
        case 8:
            self.levelLabel.backgroundColor = [UIColor redColor];
            break;
        case 9:
            self.levelLabel.backgroundColor = [UIColor purpleColor];
            break;
        case 10:
            self.levelLabel.backgroundColor = [UIColor blueColor];
            break;
            
        default:
            break;
    }

}
#pragma mrak - 创建label
- (void)creatLabel {
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = [UIColor redColor];
    [self.contentView addSubview:self.nameLabel];
    
    self.foodNameLabel = [[UILabel alloc] init];
    self.foodNameLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.foodNameLabel];
    
    self.commentLabel = [[UILabel alloc] init];
    self.commentLabel.font = [UIFont systemFontOfSize:14];
    self.commentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.commentLabel];
    
    self.levelLabel = [[UILabel alloc] init];
    self.levelLabel.font = [UIFont systemFontOfSize:13];
    self.levelLabel.layer.masksToBounds = YES;
    self.levelLabel.layer.cornerRadius = 5;
    self.levelLabel.textColor = [UIColor whiteColor];
    self.levelLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.levelLabel];
}
#pragma mark - 创建imageView
- (void)creatImageView {
    self.userImageView = [[UIImageView alloc] init];
    self.userImageView.layer.masksToBounds = YES;
    self.userImageView.layer.cornerRadius = 5;
    [self.contentView addSubview:self.userImageView];
    
    self.foodImageView = [[UIImageView alloc] init];
    self.foodImageView.layer.masksToBounds = YES;
    self.foodImageView.layer.cornerRadius = 5;
    [self.contentView addSubview:self.foodImageView];
    
    for (int i = 0; i < 5; i ++) {
        self.starImageView = [[UIImageView alloc] init];
        self.starImageView.tag = 300 + i;
        [self.contentView addSubview:self.starImageView];
    }
}
#pragma mark - 自定义高度
+ (CGFloat)heightForCellWithComment:(NSString *)comment {
    CGRect rect = [comment boundingRectWithSize:CGSizeMake(ScreenWidth - 10, ScreenWidth / 4) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    return rect.size.height + 5 + ScreenWidth / 10 + ScreenWidth / 20;
}
@end
